package com.apes.fn.scm.settle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.invoice.model.InvoiceItem;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.settle.model
 * @ClassName: PlatformSettleItem
 * @Author: pyb
 * @CreateTime: 2020-02-27 17:16
 * @Description: 供应商对账单明细
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PlatformSettleItem.CONST_TABLE_NAME)
public class PlatformSettleItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_platform_settle_item";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商对账单明细编码", padding = 8)
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "platform_settle_id", referencedColumnName = "id")
    @Comment("结算单号")
    private PlatformSettle platformSettle;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="product_id", referencedColumnName="id", nullable = false)
    @Comment("商品")
    private Product product;

    @Transient
    private String productName;

    public String getProductName() {
        if (product == null) return null;
        return product.getName();
    }


    @Comment("赋能商户")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company;

    @Column(name = "actual_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("结算数量")
    private double actualQty = 0;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    public ProductUom getUom() {
        return product.getUom();
    }

    @Transient
    private String uomName;

    public String getUomName() {
        if (uom == null) return null;
        return uom.getName();
    }


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率编码")
    private Tax tax;

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("单价")
    private double priceUnit = 0;

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("结算金额")
    private double amount = 0;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_id", referencedColumnName = "id")
    @Comment("账单明细行号")
    private InvoiceItem invoiceItem;

    @Transient
    private Long invoiceId;

    public Long getInvoiceId() {
        if (invoiceItem == null) return null;
        return invoiceItem.getInvoice().getId();
    }

    @Column(name = "origin", length = 30)
    @Comment("源单号")
    private String origin;

    @Column(name = "origin_business_type", length = 32, nullable = true)
    @Comment("源单类型")
    private String originBusinessType;


    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "occurrence_date")
    @Comment("发生日期")
    private Date occurrenceDate;

    @Column(name = "discount_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("折扣金额")
    private double discountAmount;


    @Column(name = "floor_price", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("供应商低价")
    private double floorPrice;
}
